import { defineStore } from 'pinia'
import { Users } from '../api/model/userSearchTypes'
import { reqSearchUsers } from '../api/userSearch'

export const useUserSearchStore = defineStore({
  id: 'userSearch',

  state: () => ({
    firstView: true,
    loading: false,
    errorMsg: '',
    users: [] as Users
  }),
  
  actions: {
    // 搜索的异步action
    async search(searchName: string) {
      this.firstView = false
      this.loading = true
      try {
        const response = await reqSearchUsers(searchName)
        this.loading = false
        this.users = response.data.items
      } catch (error: any) {
        this.loading = true
        this.errorMsg = error.message
      }
    }
  }
})